/*
 * Copyright 2013 Sigurd Randoll <srandoll@digiway.de>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.server.model.storage.provider;

import de.digiway.rapidbreeze.server.model.storage.StorageProviderDownloadClient;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *
 * @author Sigurd Randoll <srandoll@digiway.de>
 */
public class StorageDownloadTestUtil {

    private static Properties props = null;
    public static final String HOST_ACCOUNTS_FILE = "/HosterAccounts.properties";

    private static void loadProperties() {
        if (props == null) {
            try {
                InputStream is = StorageDownloadTestUtil.class.getResourceAsStream(HOST_ACCOUNTS_FILE);
                props = new Properties();
                props.load(is);
            } catch (IOException ex) {

                throw new IllegalStateException("For unittests of " + StorageProviderDownloadClient.class.getSimpleName() + " instances, you need valid usernames and password. "
                        + "This property file can contain key-value pairs having the name of the class under test with the suffix _username (e.g. ShareOnlineBizTest_username=abc) "
                        + "Those can be specified in the file " + HOST_ACCOUNTS_FILE + " in the test resources. "
                        + "and the name of the class with the suffix _password specifying the password.", ex);
            }
        }
    }

    public static boolean hasStorageDownload(Class<?> testClass) {
        if (StorageDownloadTestUtil.class.getResourceAsStream(HOST_ACCOUNTS_FILE) != null) {
            loadProperties();
            return getUsername(testClass) != null;
        }
        return false;
    }

    public static String getUsername(Class<?> testClass) {
        loadProperties();
        return props.getProperty(testClass.getSimpleName() + "_username");
    }

    public static String getPassword(Class<?> testClass) {
        loadProperties();
        return props.getProperty(testClass.getSimpleName() + "_password");
    }

    public static String getTestLink(Class<?> testClass) {
        loadProperties();
        return props.getProperty(testClass.getSimpleName() + "_testlink");
    }

    public static String getTestContent(Class<?> testClass) {
        loadProperties();
        return props.getProperty(testClass.getSimpleName() + "_testcontent");
    }
}
